import os
import datetime
import json

from catalog.models.sentinel2_l2a import Sentinel2L2AScene


def quick_test():

    dir_here = os.path.dirname(os.path.abspath(__file__))
    fpath_item = os.path.join(
        os.path.dirname(dir_here),
        "schemas",
        "examples",
        "sentinel2_l2a",
        "T48",
        "T48RVV",
        "S2B_MSIL2A_20200906T034539_N0214_R104_T48RVV_20200906T071603.SAFE.json",
    )
    info_item = json.load(open(fpath_item))
    print(info_item)

    # n = Sentinel2L2AScene.sweep_deleted_items(dry_run=False)
    # print(f"{n} items deleted")

    # check if an item exist or not
    item = Sentinel2L2AScene.get_one_item(
        "S2B_MSIL2A_20200906T034539_N0214_R104_T48RVV_20200906T071603"
    )
    print(item)
    print(item.transferred)
    item.transferred = datetime.datetime.utcnow()
    print(item.transferred)
    print(item.is_dirty())
    print(type(item.geom))
    ret = item.save()
    print(ret)
    # if item is None:
    #    print("no such a record or already deleted")
    #    print("insert such an item")
    #    inserted = Sentinel2L2AScene.insert_one_item(info_item, on_conflict="update")
    #    print(inserted)

    #    # check that item again
    #    item = Sentinel2L2AScene.get_one_item(
    #        "S2B_MSIL2A_20200906T034539_N0214_R104_T48RVV_20200906T071603"
    #    )
    #    print(item)
    # else:
    #    print("exist", item.uid, item.valid_until)

    #    # delete that item (actually just setting the valid_until to the current datetime)
    #    res = Sentinel2L2AScene.delete_one_item(item.uid)
    #    print(res)


if __name__ == "__main__":
    import fire

    fire.Fire(
        {
            "quick-test": quick_test,
        }
    )
